package com.crm.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.backend.domain.entity.RestBean;
import com.crm.backend.domain.po.BusinessRecord;
import com.crm.backend.domain.po.Services;
import com.crm.backend.service.IBusinessRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/api/v1/business-record")
public class BusinessRecordController {

    @Autowired
    private IBusinessRecordService businessRecordService;

    /**
     * 新增交往记录
     * @param businessRecord 交往记录信息
     * @return RestBean封装的响应结果
     */
    @PostMapping
    public RestBean<BusinessRecord> addBusinessRecord(@RequestBody BusinessRecord businessRecord) {
        boolean savedBusinessRecord = businessRecordService.save(businessRecord);
        return RestBean.success();
    }

    /**
     * 修改交往记录
     * @param id 交往记录编号
     * @param businessRecord 修改后的交往记录信息
     * @return RestBean封装的响应结果
     */
    @PutMapping("/{id}")
    public RestBean<BusinessRecord> updateBusinessRecord(@PathVariable(value = "id") Integer id, @RequestBody BusinessRecord businessRecord) {
        businessRecord.setBusId(id);
        boolean updatedBusinessRecord = businessRecordService.updateById(businessRecord);
        if (updatedBusinessRecord ) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "交往记录未找到");
        }
    }

    /**
     * 删除交往记录
     * @param id 交往记录编号
     * @return RestBean封装的响应结果
     */
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteBusinessRecord(@PathVariable(value = "id") Integer id) {
        boolean deleted = businessRecordService.removeById(id);
        if (deleted) {
            return RestBean.success();
        } else {
            return RestBean.failure(404, "交往记录未找到");
        }
    }

    /**
     * 查询交往记录
     * @param cusId 客户编号
     * @param page 页码
     * @param size 每页大小
     * @return RestBean封装的响应结果
     */
    @GetMapping
    public RestBean<IPage<BusinessRecord>> getBusinessRecords(@RequestParam(value = "cusId",required = false) Integer cusId,
                                                              @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                              @RequestParam(value = "size",defaultValue = "10") Integer size) {



        Page<BusinessRecord> pageQuery = new Page<>(page, size);
        // 创建查询条件
        LambdaQueryWrapper<BusinessRecord> queryWrapper = new LambdaQueryWrapper<>();
        // 如果状态不为空，则添加状态过滤条件
        if (cusId != null) {
            queryWrapper.eq(BusinessRecord::getCusId, cusId);
        }
        // 执行分页查询
        IPage<BusinessRecord> businessRecordPage = businessRecordService.page(pageQuery, queryWrapper);
        // 封装分页结果并返回
        return RestBean.success(businessRecordPage);
    }
}
